﻿using System;
using System.IO;
using System.Xml;
using System.Web;
using SyCODE.Component.DataFactory;
using System.Collections.Specialized;
using SyCODE.Component.Security;
using SyCODE.Component.Cryptography;
using SyCODE.Blog.Task;
using System.Web.Security;

/// <summary>
/// SyCODE 的摘要说明
/// </summary>
namespace SyCODE.Blog
{
    public class Stat : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
			Ticket ticket = Ticket.Get(FormsAuthentication.FormsCookieName);
            if (Ticket.Authenticate(ticket))
            {
                NameValueCollection nvc = new NameValueCollection(context.Request.QueryString);
                nvc.Add("account", ticket.Name);
                nvc.Add("name", HttpUtility.HtmlDecode(ticket.UserData));

                Task.Processor.Assign(new VisitorTask(nvc));
            }
            else
            {
                Ticket.Remove("user");
                context.Response.Clear();
            }

            context.Response.ContentType = "text/xml";

            DBAM dbam = new DBAM();
            dbam.BuildParameters("stat", context.Request.QueryString, false);

            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);
            writer.WriteStartDocument();

            dbam.ExecuteXmlTextWriter(writer);
            dbam.Dispose();

            writer.WriteEndDocument();

            writer.Flush();
        }
        #endregion
    }
}